package com.bc004346.travelus;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.bc004346.travelus.Adapter.PassengerAdapter;
import com.bc004346.travelus.DataAccess.AsyncTasks.GetPriceTaskFragment;
import com.bc004346.travelus.DataAccess.AsyncTasks.SelectPassengerTaskFragment;
import com.bc004346.travelus.Model.Domain.Passenger;
import com.bc004346.travelus.Model.Domain.PassengerList;
import com.bc004346.travelus.Model.Domain.StringList;
import com.bc004346.travelus.Model.Interface.IGetPriceCallback;
import com.bc004346.travelus.Model.Interface.ISelectPassengerCallback;
import com.bc004346.travelus.Model.Interface.ISelectPassengerListener;
import com.bc004346.travelus.Utility.Utility;

import java.util.ArrayList;
import java.util.List;

import static com.bc004346.travelus.Utility.Utility.GET_PRICE_TAG;
import static com.bc004346.travelus.Utility.Utility.SELECT_FLIGHT_TAG;
import static com.bc004346.travelus.Utility.Utility.SELECT_PASSENGERS_TAG;
import static com.bc004346.travelus.Utility.Utility.SELECT_PASSENGER_TAG;
import static com.bc004346.travelus.Utility.Utility.log;
import static com.bc004346.travelus.Utility.Utility.onActivityStart;
import static com.bc004346.travelus.Utility.Utility.onActivityStop;

/**
 * Created by Renats on 4/1/2014.
 * Passenger selection
 */
public class PassengerSelectionActivity extends Activity
        implements
        ISelectPassengerListener,
        ISelectPassengerCallback,
        IGetPriceCallback {

    //<editor-fold desc="Members">

    private PassengerAdapter mPaxAdapter;
    private TextView mMessage;
    private FragmentManager mFragmentManager;
    private List<Passenger> mSelectedPassengers = new ArrayList<Passenger>();
    private Button mButton;

    //</editor-fold>
    //<editor-fold desc="Overrides">

    @Override
    protected void onStart() {
        super.onStart();
        onActivityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        onActivityStop(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_passenger_selection);
            ListView listView = (ListView) findViewById(R.id.activity_pax_selection_pax_list);
            mMessage = (TextView) findViewById(R.id.activity_pax_selection_textView_message);
            mButton = (Button) findViewById(R.id.activity_pax_selection_button_continue);
            mFragmentManager = getFragmentManager();

            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (noOtherWorkInProgress()) {
                        if (mSelectedPassengers.size() == 0) {
                            mMessage.setText(getString(R.string.activity_passenger_selection_textView_message_please_select));
                            return;
                        }
                        mButton.setText(getString(R.string.common_working));
                        GetPriceTaskFragment getPriceFragment =
                                GetPriceTaskFragment.newInstance(new PassengerList(mSelectedPassengers));
                        mFragmentManager.beginTransaction()
                                .add(getPriceFragment, GET_PRICE_TAG).commitAllowingStateLoss();
                    } else mMessage.setText(getString(R.string.common_please_wait));
                }
            });

            mPaxAdapter = new PassengerAdapter(this, Utility.paxSelection);
            listView.setAdapter(mPaxAdapter);

        } catch (Exception e) {
            mMessage.setText(e.getMessage());
            log(e, this);
        }
    }

    @Override
    public void selectPassenger(Passenger pax) {
        try {
            if (noOtherWorkInProgress()) {
                mButton.setText(getString(R.string.common_working));
                List<String> list = new ArrayList<String>();
                for (Passenger passenger : mSelectedPassengers)
                    list.add(passenger.getLogin());
                list.add(pax.getLogin());
                SelectPassengerTaskFragment fragment =
                        SelectPassengerTaskFragment.newInstance(pax, new StringList(list));
                mFragmentManager.beginTransaction()
                        .add(fragment, SELECT_PASSENGER_TAG).commitAllowingStateLoss();
            } else
                mMessage.setText(getString(R.string.common_please_wait));
        } catch (Exception e) {
            mMessage.setText(e.getMessage());
            log(e, this);
        }
    }

    @Override
    public void onSelectPassengerPostExecute(Passenger pax, Exception e) {
        try {
            mMessage.setText("");
            mButton.setText(getString(R.string.common_continue));
            removeSelectPassengerFragment();

            if (e != null) {
                mMessage.setText(e.getMessage());
                mPaxAdapter.deselectPassenger(pax);
                log(e, this);
            }
            mSelectedPassengers.add(pax);
        } catch (Exception e1) {
            mMessage.setText(e1.getMessage());
            log(e, this);
        }
    }

    @Override
    public void onGetPricePostExecute(float price, Exception e) {
        try {
            removeGetPriceFragment();

            if (e != null) {
                mMessage.setText(e.getMessage());
                log(e, this);
                return;
            }

            if (price != 0) {
                mMessage.setText("We can only book free flights right now");
                return;
            }

            Utility.paxSelection = mSelectedPassengers;
            Intent intent = new Intent(this, TermsActivity.class);
            startActivity(intent);
            finish();

        } catch (Exception e1) {
            mMessage.setText(e1.getMessage());
            log(e, this);
        }
    }

    //</editor-fold>

    //<editor-fold desc="Private Methods">

    private void removeSelectPassengerFragment() {
        SelectPassengerTaskFragment fragment =
                (SelectPassengerTaskFragment) mFragmentManager.findFragmentByTag(SELECT_PASSENGER_TAG);
        if (fragment != null)
            mFragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss();
    }

    private void removeGetPriceFragment() {
        GetPriceTaskFragment fragment =
                (GetPriceTaskFragment) mFragmentManager.findFragmentByTag(GET_PRICE_TAG);
        if (fragment != null)
            mFragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss();
    }

    private boolean noOtherWorkInProgress() {

        SelectPassengerTaskFragment f1;
        GetPriceTaskFragment f2;
        f1 = (SelectPassengerTaskFragment) mFragmentManager.findFragmentByTag(SELECT_FLIGHT_TAG);
        f2 = (GetPriceTaskFragment) mFragmentManager.findFragmentByTag(SELECT_PASSENGERS_TAG);

        return (f1 == null && f2 == null);
    }
    //</editor-fold>
}
